package com.gjh.service.system.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gjh.dao.system.UserDao;
import com.gjh.domain.system.User;
import com.gjh.service.system.UserService;
import com.gjh.utils.MailUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private MailUtils mailUtils;


    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }
//保存公司信息
    @Override
    public void save(User user) {
        String password_ming = user.getPassword();
        String password = user.getPassword();
        password= new Md5Hash(password, user.getEmail(), 2).toString();
        user.setPassword(password);
        userDao.save(user);
        /*Map<String,String> map = new HashMap();
        map.put("to",user.getEmail());
        map.put("subject","恭喜新员工");
        map.put("content","您现在可以使用SaaS货代云平台了，登录密码是："+password_ming);
        amqpTemplate.convertAndSend("user.insert",map);*/

        mailUtils.sendEmail(user.getEmail(),"恭喜新员工","您现在可以使用SaaS货代云平台了，登录密码是："+password_ming);
    }

    @Override
    public void update(User user) {
//        String password = user.getPassword();
//        password= new Md5Hash(password, user.getEmail(), 2).toString();
//        user.setPassword(password);
        userDao.update(user);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public void deleteById(String id) {
        userDao.deleteById(id);
    }

    @Override
    public PageInfo findByPage(String companyId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list=userDao.findAll(companyId);
        return new PageInfo(list,5);
    }

    @Override
    public List<String> findRoleIdsByUserId(String id) {
        List<String> RoleList=userDao.findRoleIdsByUserId(id);
        return RoleList;
    }

    @Override
    public void changeRole(String userId, String[] roleIds) {
        //删除原有的用户权限
        userDao.deleteRoleIdsByUserId(userId);
        //修改
        for (String roleId : roleIds) {
            userDao.saveUserAndRole(userId,roleId);
        }
    }

    @Override
    public User findByEmail(String email) {
        User user=userDao.findByEmail(email);
        return user;
    }

    @Override
    public List<User> findAllUser() {
        return userDao.findAllUser();
    }


    public static void main(String[] args) {
        String password_page = new Md5Hash("123", "laowang@export.com", 2).toString();
        System.out.println(password_page);
        System.out.println("你好");
    }

}
